package com.aiss.ws.httptools;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class Encrypt {
//	private static final Logger LOG = LoggerFactory.getLogger(Encrypt.class);

	public static String encodeURI(String str) {
		try {
			return URLEncoder.encode(str, "UTF-8");
		} catch (Exception e) {
		}
		return str;
	}

	public static String decodeURI(String str) {
		try {
			return URLDecoder.decode(str, "UTF-8");
		} catch (Exception e) {
		}
		return str;
	}

	public static String encodeMD5(String str) {
		String temp = encodemd5(str);
		if (temp != null) {
			return temp.substring(8, 24);
		}
		return temp;
	}

	public static String encodemd5(String str) {
		MessageDigest md = null;
		String dstr = null;
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			byte[] b = md.digest();
			StringBuffer buf = new StringBuffer("");
			for (int i : b) {
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			dstr = buf.toString();
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
		return dstr;
	}

	public static String encodeDes(String strIn) {
		try {
			Cipher cipher = getDesCipher("encrypt");
			return byteArr2HexStr(cipher.doFinal(strIn.getBytes()));
		} catch (Exception e) {
//			LOG.error("加密DES[" + strIn + "]失败", e);
		}
		return null;
	}

	public static String decodeDes(String strIn) {
		try {
			Cipher cipher = getDesCipher("decode");
			return new String(cipher.doFinal(hexStr2ByteArr(strIn)));
		} catch (Exception e) {
//			LOG.error("解密DES[" + strIn + "]失败", e);
		}
		return null;
	}

	public static String decodeDes(String key, String strIn) {
		try {
			Cipher cipher = getDesCipher(key, "decode");
			return new String(cipher.doFinal(hexStr2ByteArr(strIn)));
		} catch (Exception e) {
//			LOG.error("解密DES[" + strIn + "]失败", e);
		}
		return null;
	}
	
	private static Cipher getDesCipher(String key, String type) {
		Cipher cipher = null;
		try {
			DESKeySpec dks = new DESKeySpec(key.getBytes());

			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

			SecretKey secretKey = keyFactory.generateSecret(dks);
			cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
			if ("encrypt".equals(type))
				cipher.init(1, secretKey);
			else
				cipher.init(2, secretKey);
		} catch (Exception e) {
//			LOG.error("初始化DES-Cipher失败", e);
		}
		return cipher;
	}

	private static Cipher getDesCipher(String type) {
		Cipher cipher = null;
		try {
			DESKeySpec dks = new DESKeySpec("*&_deploy^_^360_&".getBytes());

			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

			SecretKey secretKey = keyFactory.generateSecret(dks);
			cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
			if ("encrypt".equals(type))
				cipher.init(1, secretKey);
			else
				cipher.init(2, secretKey);
		} catch (Exception e) {
//			LOG.error("初始化DES-Cipher失败", e);
		}
		return cipher;
	}

	public static String byteArr2HexStr(byte[] arrB) throws Exception {
		int iLen = arrB.length;
		StringBuffer sb = new StringBuffer(iLen * 2);
		for (int i = 0; i < iLen; i++) {
			int intTmp = arrB[i];
			while (intTmp < 0) {
				intTmp += 256;
			}
			if (intTmp < 16) {
				sb.append("0");
			}
			sb.append(Integer.toString(intTmp, 16).toUpperCase());
		}
		return sb.toString();
	}

	private static byte[] hexStr2ByteArr(String strIn) throws Exception {
		byte[] arrB = strIn.getBytes();
		int iLen = arrB.length;

		byte[] arrOut = new byte[iLen / 2];
		for (int i = 0; i < iLen; i += 2) {
			String strTmp = new String(arrB, i, 2);
			arrOut[(i / 2)] = (byte) Integer.parseInt(strTmp, 16);
		}
		return arrOut;
	}
	
	public static String MD5(String sign) {
		
		byte[] bytes = null;

		try 
	    {
	        MessageDigest md5 = MessageDigest.getInstance("MD5");
	        bytes = md5.digest(sign.toString().getBytes("UTF-8"));
	    } 
	    catch (Exception e)
	    {
	        e.printStackTrace();
	        return null;
	    }
	    // 将MD5输出的二进制结果转换为小写的十六进制
	    StringBuilder sign_s = new StringBuilder();
	    
	    for (int i = 0; i < bytes.length; i++) 
	    {
	        String hex = Integer.toHexString(bytes[i] & 0xFF);
	        
	        if (hex.length() == 1)
	        {
	        	sign_s.append("0");
	        }
	        
	        sign_s.append(hex);     
	        
	    }
	    
		return sign_s.toString();
	}
}
